<template src="./template.html"></template>

<script>
import ResizeMixin from 'mixins/resize'
import { PAGE_CONFIG, FUN_IDS_OPTIONS, EXERCISE_TYPE } from 'common/consts';
import { PASSING_STATUS } from './consts';
import { getAllExercise, getExerciseSearch } from '../models';
import { formatTime as formatTimeUtil } from '@/js/utils/index';
import dayjs from 'dayjs'

export default {
  mixins: [ResizeMixin],
  data() {
    return {
      PASSING_STATUS,
      FUN_IDS_OPTIONS,
      EXERCISE_TYPE,
      isPageLoading: false,
      tableList: [],
      PAGE_CONFIG: PAGE_CONFIG,
      pageNo: PAGE_CONFIG.defaultPageNo,
      pageSize: PAGE_CONFIG.defaultPageSize,
      totalCount: 0,
      filterForm: {
        courseId: '',
        lessonId: '',
        exerciseType: '',
        passingState: ''
      },

      courseList: [],
      lessonList: []
    };
  },
  computed: {
    userInfo() {
      return this.$store.state.user.userInfo
    }
  },

  methods: {
    $handleReport(it) {
      this.$router.push({
        name: 'exercise.list.report',
        query: {
          exerciseId: it.exerciseId,
          lessonName: it.lessonName,
          lessonId: it.lessonId,
          courseId: it.courseId,
          exerciseType: it.exerciseType
        }
      })
    },
    $courseChange() {
      this.filterForm.lessonId = ''
      this.lessonList = []
      const selectedCourse = this.courseList.find(item => item.courseId === this.filterForm.courseId)
      if (selectedCourse) {
        this.lessonList = selectedCourse.lessons || []
      }
      this.$search()
    },
    $search() {
      this.pageNo = 1
      this.$getTableList()
    },
    $handleLessonData(row) {
      this.$router.push({
        name: 'curriculum.manager.lesson-data',
        params: {
          id: row.id
        }
      })
    },
    async $getExerciseSearch() {
      try {
        const { data } = await getExerciseSearch();
        this.courseList = data || []
      } catch (error) {
        this.$message.error(error.message || '获取信息失败');
      }
    },
    async $getTableList() {
      this.isPageLoading = true;
      const params = {
        ...this.filterForm,
        limit: this.pageSize,
        page: this.pageNo,
      };
      try {
        const { data } = await getAllExercise(params);
        this.tableList = (data.list || []).map(item => ({
          ...item,
          deadlineTime: item.isLimitTime === '0' ? '永久' : item.deadlineTime,
          numProcess: item.studentNum ? ((item.hasFinishStuNum / item.studentNum) * 100).toFixed(2) : 0
        }))
        this.totalCount = data.total
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.message || err.message || '获取机构列表出错')
      }
    },
    $handlePageChange() {
      this.$getTableList();
    },
    formatTime(seconds) {
      return formatTimeUtil(Number(seconds))
    },
    getTimeStr(time) {
      if (time) {
        // 格式化时间
        return dayjs(time).format('YYYY-MM-DD HH:mm')
      } else return '--'
    }
  },
  created() {
    this.$search()
    this.$getExerciseSearch()
  },
};
</script>
